import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { UserInfo } from '@/type/user'
import {API_getUserInfo} from '@/service/user'
// 定义 Store
export const useUserStore = defineStore(
  'userInfo',
  () => {
    // 用户信息
    const userInfo = ref<UserInfo>();
    const setUserInfo = (val: UserInfo) => {
        userInfo.value = val
    }
    const getUserInfo = async () => {
        let result = await API_getUserInfo();
        if (result.code) {
            userInfo.value = result.data;               
        } 
    }
    // 清理会员信息，退出时使用
    const clearUserInfo = () => {
      userInfo.value = undefined
      }

    // 记得 return
      return {
      userInfo,
      setUserInfo,
          clearUserInfo,
        getUserInfo
    }
  },
  // TODO: 持久化
  {
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value) // [!code warning]
        },
        getItem(key) {
          return uni.getStorageSync(key) // [!code warning]
        },
      },
    },
  },
)